package com.mvd.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.storage.StorageManager;
import android.util.Log;

import com.mvd.app.R;
import com.mvd.app.ac2019.FirstActivity;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.SystemUtil;

import java.lang.ref.WeakReference;
import java.lang.reflect.Method;

//欢迎页
public class WelcomeActivity extends BaseActivity {

    public static String[] permissionArray = new String[]{
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.ACCESS_FINE_LOCATION,
            android.Manifest.permission.CAMERA,
            android.Manifest.permission.READ_CALENDAR,
            android.Manifest.permission.BODY_SENSORS,

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_welcome);
        PDALogUtils.checkPhotoSetting();


        PermissionUtils.checkPermissionArray(this, permissionArray, 2);

        if (checkMounted(WelcomeActivity.this, "/mnt/sdcard")) {
            Log.e("tag", "内置SD卡可用");
        } else {
            Log.e("tag", "内置SD卡cccc可用");
        }

        if (checkMounted(WelcomeActivity.this, "/mnt/extsd")) {
            Log.e("tag", "外置SD卡可用");
        } else {
            Log.e("tag", "外置SD卡bbbbbb可用");
        }

        Log.e("tag", "手机厂商==="+SystemUtil.getDeviceBrand());
        Log.e("tag", "手机型号==="+SystemUtil.getSystemModel());
        Log.e("tag", "系统版本号==="+SystemUtil.getSystemVersion());
        String logStr = "\n" + "===手机信息==="+  "\n" +"手机厂商==="+SystemUtil.getDeviceBrand()+
        "\n" +"手机型号==="+SystemUtil.getSystemModel()+ "\n" +"系统版本号==="+SystemUtil.getSystemVersion();
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());

//        String sdFile = Environment.getExternalStorageDirectory().toString();
//        Log.e("tag", "sdFile==" + sdFile);
//        String state = Environment.getExternalStorageState();
//        Log.e("tag", "state==" + state);
//        Log.e("tag", "Environment.MEDIA_MOUNTED==" + Environment.MEDIA_MOUNTED);
//        if (state.equals(Environment.MEDIA_MOUNTED)) {
//            Log.e("tag", "=========_==========");
//        } else {
//            Toast.makeText(this, "请确认已经插入SD卡111111", Toast.LENGTH_SHORT).show();
//            Log.e("tag", "请确认已经插入SD卡");
//        }


        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                Message msg = new Message();
                msg.what = 0;
                myhandler.sendMessage(msg);
            }
        };
        myhandler.postDelayed(runnable, 2000);// 打开定时器，执行操作
    }

    /**
     * 检查是否挂载
     */
    public static boolean checkMounted(Context context, String mountPoint) {
        if (mountPoint == null) {
            return false;
        }
        StorageManager storageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);
        try {
            Method getVolumeState = storageManager.getClass().getMethod(
                    "getVolumeState", String.class);
            String state = (String) getVolumeState.invoke(storageManager,
                    mountPoint);
            return Environment.MEDIA_MOUNTED.equals(state);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    private Myhandler myhandler = new Myhandler(this);

    private static class Myhandler extends Handler {

        private final WeakReference<WelcomeActivity> weakReference;

        public Myhandler(WelcomeActivity welcomeActivity) {
            weakReference = new WeakReference<>(welcomeActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            WelcomeActivity welcomeActivity = weakReference.get();
            if (welcomeActivity != null) {


                if (!SharedPreferencesUtils.getFirstUseApp(welcomeActivity)) {
                    // 第一次执行则关闭定时执行操作
                    Intent intent = new Intent(welcomeActivity, VerifyCodeActivity.class);
                    welcomeActivity.startActivity(intent);
                } else {
                    String imei = CommonUtils.getIMEI(welcomeActivity);
                    boolean result = CommonUtils.checkAuthorizationCode(imei + "999",
                            SharedPreferencesUtils.getShouQuanMa(welcomeActivity));
                    if (result) {
                        Intent intent = new Intent(welcomeActivity, com.mvd.app.ac2019.LoginActivity.class);
////                        Intent intent = new Intent(welcomeActivity, FirstActivity.class);
                        welcomeActivity.startActivity(intent);
                    } else {
                        Intent intent = new Intent(welcomeActivity, VerifyCodeActivity.class);
                        welcomeActivity.startActivity(intent);
                    }

                }
//                Intent intent = new Intent(welcomeActivity, FirstActivity.class);
//                welcomeActivity.startActivity(intent);
                welcomeActivity.finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myhandler.removeCallbacksAndMessages(null);
    }


}
